package com.simple.cloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.simple.cloud.pojo.Dept;
import com.simple.cloud.service.IDeptService;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <h3>springcloud</h3>
 * <p></p>
 *
 * @author : zhengyue
 * @date : 2020-05-19 14:10
 **/
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private IDeptService deptService;

    @GetMapping("/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = deptService.getOne(new QueryWrapper<Dept>().lambda()
            .eq(Dept::getDeptno, id));
        if(dept == null) {
            throw new RuntimeException("id=>"+id+",不存在该用户");
        }
        return dept;
    }

    public Dept hystrixGet(@PathVariable("id") Long id) {
        return new Dept()
                .setDeptno(id)
                .setDname("id=>\"+id+\",没有对应的信息， null--@Hystrix")
                .setDbSource("no this database in MySQL");
    }

}
